package net.cmkj.mine.core.admin.sys.dict;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;

import net.cmkj.mine.bean.DictData;
import net.cmkj.mine.bean.DictType;
import net.cmkj.mine.common.anno.RequiresPermissions;
import net.cmkj.mine.common.controller.BaseController;
import net.cmkj.mine.common.model.DataGridModel;
import net.cmkj.mine.util.DataGridModelUtils;

/**
 * 数据字典管理.
 */
public class DictController extends BaseController {
	
	private DictType typeDao = DictType.dao;
	private DictData dataDao = DictData.dao;
	
	@RequiresPermissions(value={"/sys/dict"})
	public void index() throws Exception {
		render("dictType.html");
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void form() throws Exception {
		Long id = getParaToLong("id");
		if(id != null){
			setAttr("dicttype", typeDao.findById(id));
		}
		render("dictTypeForm.html");
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void dataindex() throws Exception {
		String typeValue = getPara("typeValue");
		setAttr("typeValue", typeValue);
		render("dictData.html");
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void dataform() throws Exception {
		Long id = getParaToLong("id");
		if(id != null){
			setAttr("dictdata", dataDao.findById(id));
		}
		setAttr("typeValue", getPara("typeValue"));
		render("dictDataForm.html");
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void getTypeList() throws Exception {
		try {
			String name = getPara("name");
			Page<DictType> pageInfo = typeDao.getList(getPage(), getRows(), name, getOrderby());
			DataGridModel result = DataGridModelUtils.toDataGridView(pageInfo);
			renderJson(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void getDataList() throws Exception {
		String name = getPara("name");
		String typeValue = getPara("typeValue");
		Page<DictData> pageInfo = dataDao.getList(getPage(), getRows(), name, typeValue, getOrderby());
		DataGridModel result = DataGridModelUtils.toDataGridView(pageInfo);
		renderJson(result);
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void saveType() throws Exception {
		Long id = getParaToLong("id");
		String name = getPara("name");
		String value = getPara("value");
		String des = getPara("des"); 
		renderJson(typeDao.save(id, name, value, des));
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void saveData() throws Exception {
		Long id = getParaToLong("id");
		String name = getPara("name");
		String value = getPara("value"); 
		Integer seq = getParaToInt("seq");
		String des = getPara("des"); 
		String typeValue = getPara("typeValue"); 
		renderJson(dataDao.save(id, name, value, seq, des, typeValue));
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void visibleType() throws Exception {
		Long id = getParaToLong("id");
		Integer visible = getParaToInt("visible");
		renderJson(typeDao.setVisible(id, visible));
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void visibleData() throws Exception {
		Long id = getParaToLong("id");
		Integer visible = getParaToInt("visible");
		renderJson(dataDao.setVisible(id, visible));
	}
	
	//@RequiresPermissions(value={"/sys/dict"})
	public void getData() throws Exception {
		String typeValue = getPara("keyword");
		renderJson(dataDao.getData(typeValue));
	}
	
	@RequiresPermissions(value={"/sys/dict"})
	public void getDictData() throws Exception {
		String typeValue = getPara("keyword");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type","success");
		map.put("code",0);
		map.put("content",dataDao.getData(typeValue));
		renderJson(map);
	}
	
}
